home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Languguage OS 2
/
Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO
/
language
/
embedded
/
68hc11
/
smallc11.arc
/
CC12.C
< prev
next >
Wrap
Text File
|
1988-06-27
|
7KB
|
296 lines
/*
** open an include file
**
** modified -hm ( borrowed from small-c vers 2.1 )
*/
doinclude()
{
char *cp;
blanks(); /* skip over to name */
switch(*lptr)
{
case '"':
case '<':
cp = ++lptr;
while(*cp)
{
switch(*cp)
{
case '"':
case '>':
*cp = '\0';
}
++cp;
}
}
if((input2 = fopen(lptr,"r")) == NULL)
{
input2=EOF;
error("open failure on include file");
}
kill(); /* clear rest of line so next read will come from new file (if open) */
}
/*
** test for global declarations
*/
dodeclare(class)
int class;
{
if(amatch("char",4))
{ declglb(CCHAR, class);
ns(); /* find ";" */
return YES;
}
else if((amatch("int",3)) || (class==EXTERNAL))
{ declglb(CINT, class);
ns();
return YES;
}
return NO;
}
/*** delcare a static variable*/
declglb(type, class)
int type,class;
{
int k, j;
while(1)
{ if(endst()) return; /* do line */
if(match("*"))
{ j=POINTER; /* j=POINTER */
k=0;
}
else {
j=VARIABLE; /* j=VARIABLE */
k=1;
}
if (symname(ssname, YES)==0) illname(); /* legal name?? */
if(findglb(ssname)) multidef(ssname); /* must be defined only once */
if(match("()")) j=FUNCTION; /* j=FUNCTION */
else if (match("["))
{ k=needsub(); /* get size */ /* j=ARRAY */
j=ARRAY; /* !0=array */
}
if(class==EXTERNAL) external(ssname); /* is class=EXTERNAL */
else j=initials(type>>2, j, k);
addsym(ssname, j, type, k, &glbptr, class); /* store the symbol */
if (match(",")==0) return; /* more? */
}
}
/*** declare local variables*/
declloc(typ)
int typ;
{
int k,j;
if(noloc) error("not allowed with goto");
if(declared < 0) error("must declare first in block");
while(1) {
while(1) {
if(endst()) return;
if(match("*")) j=POINTER;
else j=VARIABLE;
if (symname(ssname, YES)==0) illname();
/* no multidef check, block-locals are together */
k=BPW;
if (match("[")) {
k=needsub();
if(k) {
j=ARRAY;
if(typ==CINT)k=k<<LBPW;
}
else j=POINTER;
}
else if(match("()")) j=FUNCTION;
else if((typ==CCHAR)&(j==VARIABLE)) k=SBPC;
declared = declared + k;
addsym(ssname, j, typ, csp - declared, &locptr, AUTOMATIC);
break;
}
if (match(",")==0) return;
}
}
/*** initialize global objects*/
initials(size, ident, dim)
int size,ident,dim;
{
int savedim;
litptr=0;
if(dim==0) dim = -1;
savedim=dim;
xentry();
if(match("=")) {
if(match("{")) {
while(dim) {
init(size, ident, &dim);
if(match(",")==0) break;
}
needtoken("}");
}
else init(size, ident, &dim);
}
if((dim == -1)&(dim==savedim)) {
stowlit(0, size=BPW);
ident=POINTER;
}
dumplits(size);
dumpzero(size, dim);
return ident;
}
/*** evaluate one initializer*/
init(size, ident, dim)
int size, ident, *dim;
{
int value;
if(qstr(&value))
{ if((ident==VARIABLE)|(size!=1))
error("must assign to char pointer or array");
*dim = *dim - (litptr - value);
if(ident==POINTER) point();
}
else if(constexpr(&value))
{ if(ident==POINTER)
error("cannot assign to pointer");
stowlit(value, size);
*dim = *dim - 1;
}
}
/*
** get required array size
*/
needsub()
{
int val;
if(match("]")) return 0; /* null size */
if (constexpr(&val)==0) val=1;
if (val<0) {
error("negative size illegal");
val = -val;
}
needtoken("]"); /* force sidimension */
return val; /* and return size */
}
/*
** begin a function
**
** called from "parse" and tries to make a function
** out of the following text
**
** Patched per P.L. Woods (DDJ #52)
*/
newfunc() {
char *ptr;
nogo = /* enable goto statements */
noloc = 0; /* enable block-local declarations */
lastst= /* no statement yet */
litptr=0; /* clear lit pool */
litlab=getlabel(); /* label next lit pool */
locptr=STARTLOC; /* clear local variables */
if(monitor == YES)
fprintf(stderr,"\n%s",line);
if (symname(ssname, YES)==0) {
error("illegal function or declaration");
kill(); /* invalidate line */
return;
}
/*
ot(" xdef ");
outstr(ssname);
nl();
*/
if(func1) {
postlabel(beglab);
func1=0;
}
if(ptr=findglb(ssname)) { /* already in symbol table ? */
if(ptr[IDENT]!=FUNCTION) multidef(ssname);
else if(ptr[OFFSET]==FUNCTION) multidef(ssname);
else ptr[OFFSET]=FUNCTION;
/* earlier assumed to be a function */
}
else
addsym(ssname, FUNCTION, CINT, FUNCTION, &glbptr, STATIC);
if(match("(")==0) error("no open paren");
xentry();
locptr=STARTLOC;
argstk=0; /* init arg count */
while(match(")")==0) { /* then count args */
/* any legal name bumps arg count */
if(symname(ssname, YES)) {
if(findloc(ssname)) multidef(ssname);
else {
addsym(ssname, 0, 0, argstk, &locptr, AUTOMATIC);
argstk=argstk+BPW;
}
}
else {error("illegal argument name");junk();}
blanks();
/* if not closing paren, should be comma */
if(streq(lptr,")")==0) {
if(match(",")==0) error("no comma");
}
if(endst()) break;
}
csp=0; /* preset stack ptr */
argtop=argstk;
while(argstk) {
/* now let user declare what types of things */
/* those arguments were */
if(amatch("char",4)) {doargs(CCHAR);ns();}
else if(amatch("int",3)) {doargs(CINT);ns();}
else {error("wrong number of arguments");break;}
}
if(statement()!=STRETURN) ret();
if(litptr) {
printlabel(litlab);
dumplits(1); /* dump literals */
}
}
/*
** declare argument types
**
** called from "newfunc" this routine adds an entry in the
** local symbol table for each named argument
**
** rewritten per P.L. Woods (DDJ #52)
*/
doargs(t)
{
int j, legalname;
char c, *argptr;
while(1) {
if(argstk==0) return; /* no arguments */
if(match("*")) j=POINTER; else j=VARIABLE;
if((legalname=symname(ssname, YES))==0) illname();
if(match("[")) { /* is it a pointer? */
/* yes, so skip stuff between "[...]" */
while(inbyte()!=']') if(endst()) break;
j=POINTER; /* add entry as pointer */
}
if(legalname) {
if(argptr=findloc(ssname)) {
/* add details of type and address */
argptr[IDENT]=j;
argptr[TYPE]=t;
putint(argtop-getint(argptr+OFFSET, OFFSIZE), argptr+OFFSET, OFFSIZE);
}
else error("not an argument");
}
argstk=argstk-BPW; /* cnt down */
if(endst())return;
if(match(",")==0) error("no comma");
}
}